<template>
  <div>
    <!-- 密码找回组件 -->
    <div class="register-form">
      <div class="back"
           v-show="dvshow"
           @click="backdv">
        <i class="el-icon-back"></i>
      </div>
      <div class="dv1"
           v-show="!dvshow">
        <el-form :model="ruleForm"
                 status-icon
                 :rules="rules"
                 ref="ruleForm1"
                 class="demo-ruleForm">
          <el-form-item prop="phone"
                        class="input-width">
            <el-input type="text"
                      v-model.number="ruleForm.phone"
                      placeholder="请输入手机号码"></el-input>
          </el-form-item>
        </el-form>
        <div class="ipt">
          <el-input v-model="inputtxt"
                    @blur="handle"
                    placeholder="请输入验证码"></el-input>
          <span class="ipt1"
                style="user-select:none;"
                @click="shua">{{input}}</span>
        </div>
      </div>
      <div class="dv2"
           v-show="dvshow">
        <el-form :model="ruleForm"
                 status-icon
                 :rules="rules"
                 ref="ruleForm"
                 class="demo-ruleForm">
          <el-form-item prop="pass"
                        class="input-width">
            <el-input type="password"
                      v-model="ruleForm.pass"
                      placeholder="请输入密码"
                      autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item prop="checkPass"
                        class="input-width">
            <el-input type="password"
                      v-model="ruleForm.checkPass"
                      placeholder="请确定密码"
                      autocomplete="off"></el-input>
          </el-form-item>
        </el-form>

      </div>
      <el-button type="primary"
                 class="btn"
                 :disabled="show"
                 @click="submitForm(on)">{{txt}}</el-button>
    </div>
    <div class="register-footer">
      <div class="icon">
        <span>微信找回：</span>
        <p><i class="el-icon-chat-dot-round"></i></p>
      </div>
    </div>
  </div>
</template>

<script>
import {message ,confirm} from "@/utils/age";
import { delCookie, setCookie } from '../utils/CookieUtils'
export default {
  data () {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    // 手机号码
    var checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('邮箱不能为空'));
      } else {
        const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
        if (reg.test(value)) {
          callback();
        } else {
          return callback(new Error('请输入正确的手机号'));
        }
      }
    }
    return {
      show:true,
      on: 'ruleForm1',
      txt: "下一步",
      dvshow: false,
      ruleForm: {
        pass: '',
        checkPass: '',
        phone: ''
        // age: ''
      },
      rules: {
        pass: [
          { validator: validatePass, trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        phone: [
          { validator: checkPhone, trigger: 'blur' }
        ]
      },
      input: '',
      inputtxt: '',
      registerForm: {
        phone: '',
        password: ''
      }
    };
  },
  mounted () {
    this.vali()
  },
  methods: {
    //随机验证码  同时判别是否存在该手机号
    vali () {
      this.$http.get('random').then(response => {
        if (response) {
          // console.log(response)
          this.input = response
        }
      }).catch(err => {
      })
    },
    shua () {
      this.vali()
    },
    to () {
      this.$router.push('/home/welcome')
    },
    handle(){
      if ((this.input == this.inputtxt)&&(this.inputtxt.trim()!='')) {
        this.show=false
      }else {
        this.show=true
      }
    },
    backdv () {
      this.dvshow = false
      this.on = 'ruleForm1'
      this.txt = '下一步'
    },
    // 连接
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //发送请求判别手机号是否存在
          // this.handle()
          // 判别 下一步
          if (!this.dvshow) {
            if ((this.input == this.inputtxt)&&(this.inputtxt.trim()!='')) {
              this.dvshow = true
              this.on = 'ruleForm'
              this.txt = '提交'
              this.show=false
            } else {
              this.$message({
                message: '请输入正确的验证码！',
                type: 'error'
              })
              this.inputtxt = ""
              this.vali()
            }
          } else {
            // 提交  根据手机号修改密码
            this.registerForm.phone = this.ruleForm.phone
            this.registerForm.password = this.ruleForm.checkPass
            this.$http.post('forget', this.registerForm).then(response => {
              if (response.data) {
                message("success","修改成功，请重新登录")
                delCookie("token")
                this.$store.commit("setUsername", response.data.name)
                this.ruleForm = {}
                this.inputtxt = ""
                this.$router.push('/login')
              } else {
                message("error","改手机号未绑定用户")
                this.ruleForm.phone = ""
                this.inputtxt = ""
                this.dvshow = false
                this.on = 'ruleForm1'
                this.txt = '下一步'
                this.show=true
                this.vali()
              }
            }).catch(err => {
              this.ruleForm = {}
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
  }
}
</script>

<style>
/*  */
.back {
  width: 50px;
  height: 50px;
  /* background-color: red; */
  position: absolute;
  top: 10px;
  left: 10px;
  text-align: center;
  line-height: 50px;
  font-size: 30px;
}
.ipt {
  display: flex;
  justify-content: center;
  align-items: center;
  /* background-color: pink; */
  width: 350px;
  margin-bottom: 20px;
}
.ipt1 {
  margin-left: 30px;
  background-color: #f4f4f4;
  height: 40px;
  width: 120px;
  text-align: center;
  line-height: 40px;
  cursor: pointer;
  letter-spacing: 2px;
  border-radius: 5px;
}
/*  */
/*  */
.register-form {
  width: 100%;
  height: 280px;
  /* background-color: red; */
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
  position: relative;
  transition: all 0.4s;
}
.demo-ruleForm {
  /* background-color: pink; */
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
}
.input-width {
  width: 350px;
}
.btn {
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translate(-50%, 0);
  width: 200px;
  height: 50px;
  font-size: 20px;
}

/*  */

/*  */
.register-footer {
  width: 100%;
  height: 50px;
  /* background-color: pink; */
  border-radius: 0 0 5px 5px;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
}
.back {
  position: absolute;
  bottom: 15px;
  right: 10px;
  color: #cac0b6;
  cursor: pointer;
  transition: all 0.3s;
}
.back:hover {
  color: red;
}
.icon {
  width: 300px;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  /* background-color: red; */
}
.icon span {
  font-size: 18px;
  color: #666666;
}
.icon p {
  cursor: pointer;
  background-color: #dddddd;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  text-align: center;
  line-height: 30px;
  color: #fff;
  font-size: 20px;
  transition: all 0.3s;
}
.icon p:hover {
  background-color: #58bc46;
  color: #000;
}
/*  */
</style>

